package com.defangit.award.customization.teeth;

import com.defangit.award.domain.WinningInfo;
import com.defangit.award.extend.point.AwardListener;
import laogen.lang.exception.ParameterNotValidException;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by zhangfagen on 2019/1/4.
 */
public class TeethAwardListener extends AwardListener {
    @Override
    public boolean validateWinningInfoBeforeAward(int id,String openid,List<WinningInfo> infos) {
        if(id==2){
            LocalDateTime startTime= LocalDate.now().atStartOfDay();
            List<WinningInfo> matchs=infos.parallelStream().filter(
                info->info.getWinTime()!=null && info.getWinTime().isAfter(startTime)
            ).collect(Collectors.toList());
            if(matchs.size()>1){
                throw ParameterNotValidException.constraintNotSatisfy("抽奖次数","当日抽奖次数已用完,明日请早吧！");
            }
        }
        return true;
    }
}
